package cn.cdtu.tibetplan.controller;

import java.util.List;

import cn.cdtu.tibetplan.pojo.*;
import cn.cdtu.tibetplan.service.SchoolService;
import cn.cdtu.tibetplan.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.cdtu.tibetplan.service.RecruitmentService;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * controller
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/adver")
public class RecruitmentController {

    @Autowired
    private RecruitmentService recruitmentService;
    @Autowired
    private SchoolService schoolService;
    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<Recruitment> findAll() {
        return recruitmentService.findAll();
    }

    @RequestMapping("/list")
    public String list() {
        return "redirect:search?currentPage=1&pageSize=10";
    }

    @RequestMapping("/search")
    public String search(Recruitment recruitment, int currentPage, int pageSize, Model model) {
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools", schools);
        PageResult result = recruitmentService.findPage(recruitment, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "/Adver/index";
    }

    @RequestMapping("/addAdver")
    public String addAdver(Model model){
        List<School> list = schoolService.findAll();
        model.addAttribute("schools", list);
        return "Adver/add";
    }

    @RequestMapping("/edit")
    public String edit(Long id,Model model){
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools",schools);
        Recruitment recruitment = recruitmentService.findOne(id.longValue());
        model.addAttribute("edit",recruitment);
        return "/Adver/edit";
    }

    @RequestMapping("/show")
    public String show(Long id,Model model){
        Recruitment recruitment = recruitmentService.findOne(id.longValue());
        model.addAttribute("show",recruitment);
        return "/Adver/show";
    }
    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(int page, int rows) {
        return recruitmentService.findPage(page, rows);
    }

    /**
     * 增加
     *
     * @param recruitment
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public Result add(Recruitment recruitment) {
        try {
            recruitment.setCreatetime(Tools.getTime());
            recruitment.setUpdatetime(Tools.getTime());
            recruitment.setReadingnumber(0);
            String schoolname = schoolService.findOne(recruitment.getSchoolid().longValue()).getSchoolname();
            if (schoolname!=""){
                recruitment.setSchoolname(schoolname);
                recruitmentService.add(recruitment);
                return new Result(true, "增加成功");
            }else
            return new Result(false, "增加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @param recruitment
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public Result update(Recruitment recruitment) {
        try {
            School school = schoolService.findOne(recruitment.getSchoolid().longValue());
            recruitment.setSchoolname(school.getSchoolname());
            recruitmentService.update(recruitment);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public Recruitment findOne(Long id) {
        return recruitmentService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            recruitmentService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

}
